package com.plugin.storage.storage;

import com.plugin.storage.base.SdkAlias;
import com.plugin.storage.base.SdkCache;
import com.plugin.storage.base.SdkContext;
import com.plugin.storage.conf.SdkConfig;
import com.plugin.storage.storage.model.PlayVideoReq;
import com.plugin.storage.storage.model.UploadFileReq;
import com.plugin.storage.storage.model.UploadVideoReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;

import java.io.File;
import java.util.List;

public class Storage implements ApplicationContextAware {

    private static final Logger LOGGER = LoggerFactory.getLogger(Storage.class);

    private static volatile ApplicationContext ac;

    /**
     * 指定bucket名称
     *
     * @param bucket
     * @return
     */
    public static UploadFileReq bucket(String bucket) {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.FILE));
        req.bucket(bucket);
        return req;
    }

    /**
     * 指定别名
     *
     * @param alias
     * @return
     */
    public static UploadFileReq alias(String alias) {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(alias));
        return req;
    }

    /**
     * 私有bucket文件存储
     *
     * @return
     */
    public static UploadFileReq priv() {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.PRIV_FILE));
        return req;
    }


    /**
     * 私有bucket文件存储
     *
     * @param file
     * @return
     */
    public static UploadFileReq priv(File file) {
        UploadFileReq req = new UploadFileReq(file);
        req.setContexts(getContext(SdkAlias.PRIV_FILE));
        return req;
    }

    /**
     * 私有bucket文件存储
     *
     * @param multipartFile
     * @return
     */
    public static UploadFileReq priv(MultipartFile multipartFile) {
        UploadFileReq req = new UploadFileReq(multipartFile);
        req.setContexts(getContext(SdkAlias.PRIV_FILE));
        return req;
    }

    /**
     * 私有bucket
     *
     * @param key
     * @return
     */
    public static UploadFileReq priv(String key) {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.PRIV_FILE));
        return req.setKey(key);
    }

    /**
     * 文件存储
     *
     * @return
     */
    public static UploadFileReq file() {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.FILE));
        return req;
    }

    /**
     * 文件存储
     *
     * @param file
     * @return
     */
    public static UploadFileReq file(File file) {
        UploadFileReq req = new UploadFileReq(file);
        req.setContexts(getContext(SdkAlias.FILE));
        return req;
    }

    /**
     * 文件存储
     *
     * @param multipartFile
     * @return
     */
    public static UploadFileReq file(MultipartFile multipartFile) {
        UploadFileReq req = new UploadFileReq(multipartFile);
        req.setContexts(getContext(SdkAlias.FILE));
        return req;
    }

    /**
     * 文件存储
     *
     * @param key
     * @return
     */
    public static UploadFileReq file(String key) {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.FILE));
        return req.setKey(key);
    }

    /**
     * 获取访问链接
     *
     * @param url
     * @return
     */
    public static String url(String url) {
        UploadFileReq req = new UploadFileReq();
        req.setContexts(getContext(SdkAlias.FILE, SdkAlias.VIDEO));
        return req.setUrl(url).url();
    }

    public static String saveUrl(String url) {
        return StorageUtils.removeQuery(url(url));
    }

    public static UploadVideoReq video() {
        UploadVideoReq req = new UploadVideoReq();
        req.setContexts(getContext(SdkAlias.VIDEO));
        return req;
    }


    public static UploadVideoReq video(File file) {
        UploadVideoReq req = new UploadVideoReq(file);
        req.setContexts(getContext(SdkAlias.VIDEO));
        return req;
    }

    public static PlayVideoReq play(String videoId) {
        PlayVideoReq req = new PlayVideoReq();
        req.setVideoId(videoId);
        req.setContexts(getContext(SdkAlias.VIDEO));
        return req;
    }

    private static List<SdkContext> getContext(String... types) {
        SdkConfig sdkConfig = ac.getBean(SdkConfig.class);
        return sdkConfig.getContext(types);
    }

    @Override
    public void setApplicationContext(ApplicationContext cx) throws BeansException {
        if (ac != null) {
            return;
        }
        synchronized (Storage.class) {
            if (ac != null) {
                return;
            }
            ac = cx;
            try {
                SdkCache.setJedis(ac.getBean(Jedis.class));
            } catch (Exception ignored) {
            }
        }
    }

}
